package backend.Instruction;

import backend.component.RISCVcomponent;
import backend.Register.RISCVoperand;
import backend.Register.RISCVreg;

import java.util.ArrayList;
import java.util.List;

public class RISCVinst extends RISCVcomponent {
    private RISCVreg reg;
    private List<RISCVreg> regUse;
    private List<RISCVreg> live;

    public RISCVinst() {
        reg = null;
        regUse = new ArrayList<>();
        live = new ArrayList<>();
    }

    private void addUse(RISCVoperand reg) {
        if (reg instanceof RISCVreg) {
            regUse.add((RISCVreg) reg);
        }
    }
    public void setDef(RISCVoperand l){
        if(l instanceof RISCVreg){
            reg = (RISCVreg)l;
        }
    }

    public void setDefReg(RISCVoperand oldreg,RISCVoperand newreg){
        if(oldreg != null && oldreg instanceof RISCVreg){
            reg = null;
        }
        setDef(newreg);
    }

    public void addUseReg(RISCVoperand oldreg,RISCVoperand newreg){
        if(oldreg != null && oldreg instanceof RISCVreg){
            regUse.remove((RISCVreg) oldreg);
        }
        addUse(newreg);
    }

    public RISCVreg getReg() {
        return reg;
    }

    public List<RISCVreg> getRegUse() {
        return regUse;
    }

    public List<RISCVreg> getLive() {
        return live;
    }
    public void replaceReg(RISCVoperand oldreg,RISCVoperand newreg){

    }


    public String toString(){
        return "Fail\n";
    }
}
